package com.taxi.driver.widget.code;

import android.content.Context;
import android.text.Editable;
import android.text.Selection;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.taxi.driver.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 功能描述：
 */
public class CodeInput extends LinearLayout {

    public CodeInput(Context context, AttributeSet attrs) {
        super(context, attrs);
        initCodeInput(context);
    }

    private List<EditText> viewList = new ArrayList<>();
    private CodeInputListener listener;

    private void initCodeInput(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.view_code_input, this, false);
        addView(view); //添加到布局中
        viewList.clear();
        int[] ids = new int[]{R.id.et0, R.id.et1, R.id.et2, R.id.et3};
        for (int i = 0; i < ids.length; i++) {
            EditText itemView = (EditText) view.findViewById(ids[i]);
            viewList.add(itemView);
            //监听输入变化
            itemView.addTextChangedListener(new CodeTextWatcher(i));
            //监听焦点变化
            itemView.setOnFocusChangeListener((v, hasFocus) -> {
                if (hasFocus) { //光标移到最后
                    Editable editable = ((EditText) v).getEditableText();
                    Selection.setSelection(editable, editable.length());
                }
            });
        }
    }

    /**
     * 输入完整时，触发回调
     */
    private void dealwithInput() {
        for (int i = 0; i < viewList.size(); i++) {
            //一旦有未输入完整的项，不执行回调
            if (viewList.get(i).getEditableText().length() < 1) return;
        }
        if (listener != null) listener.onInputComplete();
    }

    class CodeTextWatcher implements TextWatcher {

        private int index;

        public CodeTextWatcher(int index) {
            this.index = index;
        }


        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if (index > 0 && s.length() == 0) {
                viewList.get(index - 1).requestFocus(); //焦点前移
                return;
            }

            if (index < 3 && s.length() == 1)
                viewList.get(index + 1).requestFocus(); //焦点切换到下一个EditText

            dealwithInput(); //判断是否需要触发回调
        }

    }

    /**
     * 设置输入监听
     *
     * @param listener
     */
    public void setCodeInputListener(CodeInputListener listener) {
        this.listener = listener;
    }

    public interface CodeInputListener {
        void onInputComplete(); //输入完整时，将触发
    }

    /**
     * 获取输入的验证码
     *
     * @return
     */
    public String getCode() {
        StringBuilder str = new StringBuilder();
        for (int i = 0; i < viewList.size(); i++) {
            str.append(viewList.get(i).getText().toString().trim());
        }
        return str.toString();
    }

    /**
     * 清空输入
     */
    public void resetInput() {
        for (int i = 0; i < viewList.size(); i++) {
            viewList.get(i).getEditableText().clear();
        }
        viewList.get(0).requestFocus();
    }

}
